const db = require('../comment/Db')

class HallDb extends db{
    //实现单例方法
    static getInstance() {
        if(!HallDb.instance){
            HallDb.instance = new HallDb();
            return HallDb.instance;
        }else{
            return HallDb.instance;
        }
    }

    constructor() {
        super();
        //子类掉用父类的方法
        this.connectSql();
    }

    //返回一个promise   成功或者失败
    getUserInfo(id){
        return new Promise((resolve, reject) => {
            let sql = `select *from user_info where user_id = ${id}`
            console.log(sql)
            this.myQuery(sql,resolve,reject);
        })
    }

    getRoomInfo(roomID){
        return new Promise((resolve, reject) => {
            let sql = `select *from room_info where room_id = ${roomID}`
            console.log(sql)
            this.myQuery(sql,resolve,reject);
        })
    }

    queryRoomIdIsUsable(roomID){
        return new Promise((resolve, reject) => {
            let sql = `select* from room_info where room_id = ${roomID}`;
            this.myQuery(sql,resolve,reject);
        })
    }
    saveRoomInfo(room_id,room_info){
        return new Promise((resolve, reject) => {
            let jiesan = 0;
            if(!room_info.isBanShuJieSan){
                jiesan = 1;
            }
            let sql = `insert into room_info(room_id,create_id,game_numbers,jie_san)values(${room_id},${room_info.createUserID},${room_info.gameNumbers},${jiesan})`;
            this.myQuery(sql,resolve,reject);
        })
    }

}

//在全局对象global上附加了一个loginServerMgr（登陆服务器管理者）  附加了一个（登陆服务器数据库管理模块）
global.hallServerMgr.hallDbMgr = HallDb.getInstance();
module.exports = HallDb;











